package com.jky.trial.common;

import lombok.Data;

import java.io.Serializable;

@Data
public class BaseResult<T> implements Serializable {
    public static final int FAIL = 4500;
    public static final int SUCCESS = 200;
    public static final int UNKNOWN_ERROR = 4506;

    private static final long serialVersionUID = -4185151304730685014L;

    protected boolean success;
    protected String error;
    /**
     * 处理结果状态码
     * 200表示成功
     */
    protected int code;
    protected int total;
    private T data;
    private Pagination page;

    public BaseResult() {
    }

    public BaseResult(boolean success, String error, int code) {
        this.success = success;
        this.error = error;
        this.code = code;
    }

    public BaseResult(boolean success, String error) {
        this.success = success;
        this.error = error;
    }

    public BaseResult(boolean success, T data) {
        this.success = success;
        this.data = data;
        this.code = 200;
    }

    public static <T> BaseResult<T> success(T data) {
        return new BaseResult(true, data.toString(), SUCCESS);
    }

    public static <T> BaseResult<T> fail(String msg) {
        return new BaseResult(false, msg, FAIL);
    }

    public static <T> BaseResult<T> unknownError(String msg) {
        return new BaseResult(false, msg, UNKNOWN_ERROR);
    }

    @Override
    public String toString() {
        return "BaseResult [success=" + success + ", data=" + data + ", error="
                + error + "]";
    }

}
